package class01;

import java.util.Arrays;

/**
 * @author zhangchaoliang
 * create 2022
 */
public class e1 {
    public static boolean exited(int[] arr, int value) {
        if (arr == null || arr.length == 0)
            return false;

        int L=0;
        int R=arr.length-1;
        int mid=0;
        while (L<R){
            mid=L+((R-L)>>1);
            if (arr[mid]==value){
                return true;
            }else if (arr[mid]>value){
                R=mid-1;
            }else {
                L=mid+1;
            }
        }
        return arr[L]==value;

    }

    public static boolean test(int[] arr,int value){
        for (int cur:arr){
            if (cur==value)
                return true;
        }
        return false;
    }

    public static int[] RandomArrays(int maxSize,int maxValue){
        int[] arr = new int[(int)((maxSize+1)*Math.random())];
        for (int i=0;i<arr.length;i++){
            arr[i]=(int)((maxValue+1)*Math.random())-(int)((maxValue+1)*Math.random());
        }
        return arr;
    }

    public static void main(String[] args) {
        int testTime = 500000;
        int maxSize=100;
        int maxVale=100;
        boolean successed = true;

        for (int i=0;i<testTime;i++){
            int[] arr = RandomArrays(maxSize,maxVale);
            Arrays.sort(arr);
            int value = (int)((maxVale+1)*Math.random())-(int)((maxVale+1)*Math.random());
            if(exited(arr,value)!=test(arr,value)){
                successed=false;
                break;

            }
        }
        System.out.println(successed?"成功！":"失败！");
    }

}
